package com.haizhi.crm.sys.auth.dao;


import com.haizhi.crm.sys.auth.model.RelationItemDo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yangyijun on 2018/4/26.
 */
@Repository
public interface RelationItemDao extends JpaRepository<RelationItemDo, Long> {

    @Query("select count(1) from RelationItemDo where relationId=:relationId" )
    Integer countByRelationId(@Param("relationId" ) Long relationId);

    void deleteByRelationId(@Param("relationId" ) Long relationId);

    List<RelationItemDo> findByEntityKey(String companyKey);

    List<RelationItemDo> findByEntityKeyAndRelationId(String companyKey, Long relationId);

    List<RelationItemDo> findByUserId(Long userId);

    @Query("select entityKey from RelationItemDo where userId in :userIds" )
    List<String> findEntityKeys(@Param("userIds" ) List<Long> userIds);


    @Query("select entityKey from RelationItemDo where relationId=:relationId and userId in :userIds" )
    List<String> findEntityKeys(@Param("relationId" ) Long relationId, @Param("userIds" ) List<Long> userIds);

    @Query("select relationId from RelationItemDo where userId =:userId and entityKey=:entityKey" )
    List<Long> findBy(@Param("userId" ) Long userId, @Param("entityKey" ) String entityKey);
}